﻿using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using B.MESUI.Login.ProdManagement.DTO;
using B.MESUI.Login.ProdManagement.Services;
namespace B.MESUI.Login.ProdManagement.Lock
{
    public partial class LockDialogForm : DevExpress.XtraEditors.XtraForm
    {
        public delegate void GetClearData();
        public event GetClearData ClearDataEvent;
        

        private string _codeid;
        private string _workorderType;

        public LockDialogForm()
        {
            InitializeComponent();
        }

        public LockDialogForm(string codeid = "", string workorderType = "") : this()
        {
            _codeid = codeid;
            _workorderType = workorderType;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            button1.Click += button1_Click;
        }

        private async void button1_Click(object sender, EventArgs e)
        {
            var request = new AddProductLockRequest
            {
                codeid = _codeid,
                workordeR_TYPE = _workorderType,
                locK_STARTTIME = locK_STARTTIME.DateTime,
                locL_ENDTIME = locL_ENDTIME.DateTime,
                locK_REASON = locK_REASON.Text
            };

            
            bool success = await ProductLockService.AddProductLockAsync(request);
            if (success)
            {
                MessageBox.Show("锁定成功！");
                ClearDataEvent?.Invoke();
                if (this.Owner is B.MESUI.Login.ProdManagement.Lock.ProductLockForm parentForm)
                {
                    parentForm.ClearData();
                }
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBox.Show("锁定失败！");
            }
        }
    }
}